package 简单.二叉树的所有路径;

import javax.xml.soap.Node;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

//广度优先探索
public class Solution {

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> ans = new ArrayList<>();
        if(root == null) return ans;



        Queue<TreeNode> NodeQueue = new LinkedList<>();
        Queue<String> PathQueue = new LinkedList<>();

        NodeQueue.offer(root);
        PathQueue.offer(Integer.toString(root.val));


        while (!NodeQueue.isEmpty()){


            TreeNode tmp = NodeQueue.poll();
            String path = PathQueue.poll();

            if(tmp.left == null && tmp.right == null){
                ans.add(path);
            }else {
                if(tmp.left != null){
                    NodeQueue.offer(tmp.left);
                    PathQueue.offer(new StringBuffer(path).append("->").append(tmp.left.val).toString());
                }

                if(tmp.right != null){
                    NodeQueue.offer(tmp.right);
                    PathQueue.offer(new StringBuffer(path).append("->").append(tmp.right.val).toString());

                }

            }


        }
        return ans;

    }
}
